import 'package:efood_multivendor/controller/product_controller.dart';
import 'package:efood_multivendor/data/model/response/order_details_model.dart';
import 'package:efood_multivendor/data/model/response/product_model.dart';
import 'package:efood_multivendor/helper/price_converter.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/custom_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../helper/route_helper.dart';
import '../../product/product_screen.dart';

class OrderProductWidget extends StatelessWidget {
  final Details details;
  OrderProductWidget({required this.details});

  @override
  Widget build(BuildContext context) {
    String getItemString(int quantity) {
      if (quantity > 1) {
        return 'free items';
      }
      return 'free item';
    }

    bool isOpen = true;
    String _addOnText = '';
    details.addOns?.forEach((addOn) {
      _addOnText = _addOnText +
          '${(_addOnText.isEmpty) ? '' : ',  '}${addOn.name} (${addOn.quantity})';
    });

    // String _variationText = '';
    // if (details.variation.length > 0) {
    //   List<String> _variationTypes = details.variation[0].type.split('-');
    //   if (_variationTypes.length == details.foodDetails.choiceOptions.length) {
    //     int _index = 0;
    //     details.foodDetails.choiceOptions.forEach((choice) {
    //       _variationText = _variationText +
    //           '${(_index == 0) ? '' : ',  '}${choice.title} - ${_variationTypes[_index]}';
    //       _index = _index + 1;
    //     });
    //   } else {
    //     _variationText = details.foodDetails.variations[0].type;
    //   }
    // }

    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      InkWell(
        onTap: () async {
          Product? product;

          try {
            product = await Get.find<ProductController>()
                .getProductDetails(Product(id: details.foodId));
            if (product == null) {
              isOpen = false;
            } else {
              isOpen = true;
            }
          } catch (e) {
            isOpen = false;
          }

          if (isOpen) {
            // Product product = Product(
            //   id: foodDetails.id,
            //   name: foodDetails.name,
            //   description: foodDetails.description,
            //   image: foodDetails.image,
            //   categoryId: foodDetails.categoryId,
            //   variations: foodDetails.variations.map((variation) => Variation(
            //       // map variation attributes here
            //       )).toList(),
            //   addOns: foodDetails.addOns,
            //   choiceOptions:
            //       foodDetails.choiceOptions.map((choiceOption) => ChoiceOptions(
            //           // map choice option attributes here
            //           )).toList(),
            //   price: foodDetails.price,
            //   tax: foodDetails.tax,
            //   discount: foodDetails.discount,
            //   discountType: foodDetails.discountType,
            //   availableTimeStarts: foodDetails.availableTimeStarts,
            //   availableTimeEnds: foodDetails.availableTimeEnds,
            //   restaurantId: foodDetails.restaurantId,
            //   restaurantName: foodDetails.restaurantName,
            //   restaurantDiscount: foodDetails.restaurantDiscount,
            //   restaurantOpeningTime: foodDetails.restaurantOpeningTime,
            //   restaurantClosingTime: foodDetails.restaurantClosingTime,
            //   scheduleOrder: foodDetails.scheduleOrder,
            //   avgRating: foodDetails.avgRating,
            //   ratingCount: foodDetails.ratingCount,
            //   veg: foodDetails.veg,
            //   restaurantTooFar: foodDetails.restaurantTooFar,
            //   is_open: foodDetails.is_open,
            //   is_alcohol: foodDetails.is_alcohol,
            //   isHalal: foodDetails.isHalal,
            // );
            // print(product.addOns[0].toJson().toString());
            Get.toNamed(RouteHelper.getProductRoute(details.foodDetails!.id!),
                arguments: ProductScreen(
                  product: product!,
                ));
          }
        },
        child: Row(children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL),
            child: CustomImage(
              height: 50,
              width: 50,
              fit: BoxFit.cover,
              image: details.foodDetails!.image!,
            ),
          ),
          SizedBox(width: Dimensions.PADDING_SIZE_SMALL),
          Expanded(
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Row(children: [
                Expanded(
                    child: isOpen
                        ? Text(
                            details.foodDetails?.name ?? '',
                            style: sfMedium.copyWith(
                                fontSize: Dimensions.fontSizeSmall),
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          )
                        : Text(
                            details.foodDetails?.name ?? '',
                            style: sfMedium.copyWith(
                                fontSize: Dimensions.fontSizeSmall,
                                color: Theme.of(context).disabledColor),
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          )),
                Text('${'quantity'.tr}: ',
                    style:
                        sfRegular.copyWith(fontSize: Dimensions.fontSizeSmall)),
                Text(
                  details.quantity.toString(),
                  style: sfMedium.copyWith(
                      color: Theme.of(context).primaryColor,
                      fontSize: Dimensions.fontSizeSmall),
                ),
              ]),
              SizedBox(height: Dimensions.PADDING_SIZE_EXTRA_SMALL),
              Text(
                PriceConverter.convertPrice(details.price?.toDouble() ?? 0),
                style: sfMedium,
              ),
              SizedBox(height: Dimensions.PADDING_SIZE_EXTRA_SMALL),
              details.freeQuantity! > 0
                  ? Text(
                      '+' +
                          details.freeQuantity.toString() +
                          ' ' +
                          getItemString(details.freeQuantity!),
                      style: sfMedium,
                    )
                  : SizedBox(),
            ]),
          ),
        ]),
      ),
      _addOnText.isNotEmpty
          ? Padding(
              padding:
                  EdgeInsets.only(top: Dimensions.PADDING_SIZE_EXTRA_SMALL),
              child: Row(children: [
                SizedBox(width: 60),
                Text('${'addons'.tr}: ',
                    style:
                        sfMedium.copyWith(fontSize: Dimensions.fontSizeSmall)),
                Flexible(
                    child: Text(_addOnText,
                        style: sfRegular.copyWith(
                          fontSize: Dimensions.fontSizeSmall,
                          color: Theme.of(context).disabledColor,
                        ))),
              ]),
            )
          : SizedBox(),
      // details.foodDetails.variations.length > 0
      //     ? Padding(
      //         padding:
      //             EdgeInsets.only(top: Dimensions.PADDING_SIZE_EXTRA_SMALL),
      //         child: Row(children: [
      //           SizedBox(width: 60),
      //           Text('${'variations'.tr}: ',
      //               style:
      //                   sfMedium.copyWith(fontSize: Dimensions.fontSizeSmall)),
      //           Flexible(
      //               child: Text(_variationText,
      //                   style: sfRegular.copyWith(
      //                     fontSize: Dimensions.fontSizeSmall,
      //                     color: Theme.of(context).disabledColor,
      //                   ))),
      //         ]),
      //       )
      //     : SizedBox(),
      Divider(height: Dimensions.PADDING_SIZE_LARGE),
      SizedBox(height: Dimensions.PADDING_SIZE_SMALL),
    ]);
  }
}
